﻿using B.S.DataBase.MyProject.ErrorCode;
using B.S.Traceability.Domain;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Write.API.Applicantion.Command.GoodsCommand;
using MediatR;

namespace B.S.Traceability.Write.API.Applicantion.CommandHandler.GoodsCommandHandler
{
    public class DelGoodsCommandHandler : IRequestHandler<DelGoodsCommand, APIResult<object>>
    {
        private readonly IBaseRepository<SU_GoodsInfo> goodsRep;

        public DelGoodsCommandHandler(IBaseRepository<SU_GoodsInfo> goodsRep)
        {
            this.goodsRep = goodsRep;
        }

        public async Task<APIResult<object>> Handle(DelGoodsCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var goodsIdslist = goodsRep.GetAll().Result.Where(x=>request.GoodsIds.Contains(x.GoodsId)).ToList();
            foreach(var item in goodsIdslist)
            {
                item.IsDel = true;
                var res = goodsRep.UpdateRanhes(goodsIdslist).Result;
            }
            result.Code = APIEnum.删除成功;
            result.Msg = APIEnum.删除成功.ToString();
            return await Task.FromResult(result);



        }
    }
}
